//
//  HospitalViewController.m
//  DrProcessTable
//
//  Created by 白岩凇 on 2016/10/3.
//  Copyright © 2016年 白岩凇. All rights reserved.
//

#import "HospitalViewController.h"
#import "MJExtension.h"
#import "HsptCellModel.h"
#import "HospitalCell.h"
#import "DepartmentViewController.h"
#import "GHNetWorkTools.h"


#define navigationTitle @"选择医院"
@interface HospitalViewController ()


@end

@implementation HospitalViewController


-(NSMutableArray *)models{
    //if (!_models) {
       // NSString* path=[[NSBundle mainBundle]pathForResource:@"HsptInfo.plist" ofType:nil];
        //字典数组
       // NSArray* HsptInfoArray=[NSArray arrayWithContentsOfFile:path];
        
        
        
        
        
        
       // NSLog(@"%@",HsptInfoArray);
    //临时模型数组
     NSLog(@"p----%@",_models);
        NSMutableArray* modelsArray=[NSMutableArray array];
        //字典数组转模型数组 mjExtension框架
        modelsArray=[HsptCellModel mj_objectArrayWithKeyValuesArray:_models];
   // modelsArray=[HsptCellModel mj_objectArrayWithKeyValuesArray:HsptInfoArray];
        _models=modelsArray;
        
//        NSLog(@"--------------\n %@ \n-------------------",_models);
    //}
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=navigationTitle;
    self.tableView.separatorInset=UIEdgeInsetsZero;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear #import "AFNetworking.h"= NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 数据源方法
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.models.count;
}

//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HospitalCell *cell = [HospitalCell cellWithTableView:tableView];
    cell.model=self.models[indexPath.row];
    
    // Configure the cell...
    
    return cell;
}

/**
 *  返回每一行的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HsptCellModel* need=self.models[indexPath.row];
    CGFloat needH=need.h;
    return needH;
}


/**
 *  估计cell的高度,改变执行顺序。优化性能。
 *  @param tableView tableView description
 *  @param indexPath indexPath description
 *
 *  @return 估计的高度
 *  本来应该是先获得cell的准确高度，再去搞cell返回给tableView。有了这个方法就会先得到预估的cell高度去搞cell返回，再获得cell的准确高度。
 
 */
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    return 200;
}

#warning 进入科室选择界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //创建DepartmentViewController,push进导航控制器
    
   
    HospitalCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
   // NSString *urlstr = [NSString stringWithFormat:@"department?HospitalName=%@",cell.model.HospitalName];
    NSString *urlstr = @"department";
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell.model.HospitalName,@"HospitalName", nil];
    NSLog(@"%@",urlstr);
    [[GHNetWorkTools sharedTools] requestDataWith:GET urlString:urlstr parameters:dict finishedBlock:^(id result, NSError *error) {
        NSLog(@"%@",result);
        
        HospitalCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        
        DepartmentViewController* need=[[DepartmentViewController alloc]init];
        
        need.models = [result valueForKey:@"department"];
        need.title = cell.model.HospitalName;
        //设置返回按钮样式
        self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
        [self.navigationController pushViewController:need animated:YES];

        
        
    }];
    
    
    
    
    
    
}

@end
